/**
 * Tree面板，对ZTree进行第二次封装
 * @author mayonghua
 * @param config
 * @return
 */
var TreePanel = function(config){
	this.renderTo = config.renderTo;
	this.selectNode = config.selectNode==null?"":config.selectNode;//要选择的节点ID
	this.expandNode = config.expandNode==null?"":config.expandNode;//要展开的节点ID
	this.click = config.click;//点击时的回调函数
	this.treeNodeKey = config.treeNodeKey==null?"id":config.treeNodeKey;//节点的标识
	this.treeNodeParentKey = config.treeNodeParentKey==null?"pId":config.treeNodeParentKey;//节点的父标识
	this.treeNodes = config.treeNodes;//将要添加到zTree的数组
	this.load = config.load;//该控件初始化的时候是否载入相关页面
	this.back = config.back;//当点击后退按钮时执行的 方法
	this.init();
}
TreePanel.prototype = {
	init : function(){
		var entity = this;
		entity.windowResize();
		$(window).resize(function(){
		entity.windowResize();
		});
		entity.drawTree();
		if(entity.back){
		//为返回按钮添加事件
		$(".treePanel .back").click(entity.back);
		}
	},
	windowResize : function(){
		var entity = this;
		//当父窗口改变大小的时候，改变当前窗口的大小
		var height = $(".treePanel").parent().height();
		$(".treePanel").height(height-2);//减去2个像素的border
		//计算顶部的高度
		var panelHead = $("div.treePanel .head").height();
		//计算底部的高度
		var panelBottom = $("div.treePanel .start").height();
		//计算上部分类的高度
		var categoryHeight = $("div.treePanel .system").height();
		var contentHeight = height-panelHead-panelBottom-categoryHeight-3;
		$("div.treePanel .treeContent").height(contentHeight);
	},
	drawTree:function(){
		var entity = this;
		var zTree;
		var setting = {
			isSimpleData: true,//设置为简单的数组格式
			treeNodeKey: entity.treeNodeKey,
			treeNodeParentKey: entity.treeNodeParentKey,
			showLine: true,
			callback:{
				click:entity.click
			},
			root:{ 
				isRoot:true,
				nodes:[]
			}
		};
		setting.expandSpeed = ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast";
		zTree = $("#"+entity.renderTo).zTree(setting, entity.treeNodes);
		//选择某个节点
		if(entity.selectNode!=null){
		var node = zTree.getNodeByParam("id",entity.selectNode);
		zTree.selectNode(node);
		}
		//展开某个节点
		if(entity.expandNode!=null){
		var node = zTree.getNodeByParam("id",entity.expandNode);
		zTree.expandNode(node,true,true);
		}
		if(entity.load){
		entity.load(zTree);
		}
	}
}